package com.liu.user.controller;

import com.liu.common.entity.PageResult;
import com.liu.common.entity.Result;
import com.liu.common.status.CodeEnum;
import com.liu.user.client.LabelClient;
import com.liu.user.entity.User;
import com.liu.user.service.UserService;
import com.sun.org.apache.regexp.internal.RE;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.apache.bcel.classfile.Code;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;


/**
 * Created by Administrator on 2019/8/15 0015.
 * @RestController 表示该控制器所有的返回均为josn格式的对象
 * @RequestMapping("/user") 表示该控制器作用域为/user
 * @Slf4j 生成log的内置对象，用于记录日志
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private LabelClient labelClient;


    @GetMapping("/label")
    public Result findLableAll(){
       Result result =labelClient.findAll();
        System.out.println("我进入该方法了");
        return result;
    }


    /**
     * @GetMapping(value = "/{id}") 表示书写接口的风格为restful风格 ，调用的方式为get，同时，使用路径参数
     * @param id
     * @return
     */
    @GetMapping(value = "/{id}")
    public Result findById(@PathVariable("id") String id){

        User user = userService.findById(id);
        return new Result(true,1,"查询成功",user);
    }

    /**
     * @GetMapping(value = "/{id}") 表示书写接口的风格为restful风格 ，调用的方式为get，同时，使用路径参数
     * @param page rows query
     * @return
     */
    @GetMapping(value = "/{page}/{rows}/")
   public Result findByList(@PathVariable(value = "page" ,required = false) int page , @PathVariable("rows") int rows, String query){

       log.info(" page{}",page);
       log.info("rows{}",rows);
       log.info("query{}",query);
       PageResult pageResult= userService.findAllByPage(page,rows,query);

        return new Result(true,1,"查询成功",pageResult);
   }

   @PostMapping("/sendsms/{mobile}")
   public Result sendsms(@PathVariable ("mobile") String mobile){
       userService.sendSMS(mobile);

       return  new Result(true, CodeEnum.OK.getCode(),"验证码发送成功");

   }

   @PostMapping("/register/{code}")
    public Result registUser(@RequestBody User user,@PathVariable  String code){
       userService.addUser(user,code);
       log.info("用户信息：{}",user);

       return  new Result(true,CodeEnum.OK.getCode(),"用户注册成功");
   }

   @PostMapping("/login")
   public Result login(String mobile,String password){
        log.info("mobile{}",mobile);
        log.info("password{}",password);
        User user =userService.findByMobileAndPassword(mobile,password);
        if(user!=null){
            return  new Result(true, CodeEnum.OK.getCode(),"登录成功");
        }else {
            return  new Result( false,CodeEnum.LOGIN_ERROR.getCode(),CodeEnum.LOGIN_ERROR.getMessage());
        }


    }


    public Result incFanscount(String userid,int x){
       userService.incFanscount(userid, x);

       return  new Result(true,CodeEnum.OK.getCode(),"更新粉丝数成功！！");
    }

    public Result incFollowcount(String userid,int x){
        userService.incFollowcount(userid, x);
        return  new Result(true,CodeEnum.OK.getCode(),"更新粉丝数成功！！");
    }






}
